<?php

class OrganizationsAbstractList extends ResourceList
{

    protected $extension = '';
    protected $form_fiscal_id;
    protected $form_name;
    protected $form_commercial_name;
    protected $search_name;
    protected $search_fiscal_id;

    function setFormData( $fiscal_id, $name, $commercial_name, $extension )
    {
        $this->fiscal_id = $fiscal_id;
        $this->form_name = $name;
        $this->form_commercial_name = $commercial_name;
        $this->extension = $extension;


        if ( empty( $name ) ) $this->errors[ ] = 'name';

        if ( empty( $fiscal_id ) || !OrganizationAbstract::control_cif( $fiscal_id ) )
                $this->errors[ ] = 'fiscal_id';

        if ( empty( $commercial_name ) ) $this->errors[ ] = 'commercial_name';

        if ( !empty( $extension ) && !in_array( $extension,
                        array( 'PNG', 'JPEG', 'JPG', 'GIF' ) ) )
                $this->errors[ ] = 'extension';
    }

    function setSearchData( $fiscal_id, $name )
    {
        $this->search_fiscal_id = $fiscal_id;
        $this->search_name = $name;
    }

    function handlePOST( Event $e )
    {
        if ( $e->tag == 'crear' ) {
            $_POST[ 'name' ] = trim( preg_replace( '/\s+?/', ' ',
                            $_POST[ 'name' ] ) );
            $_POST[ 'commercial_name' ] = trim( preg_replace( '/\s+?/', ' ',
                            $_POST[ 'commercial_name' ] ) );
            $_POST[ 'fiscal_id' ] = strtr( $_POST[ 'fiscal_id' ],
                    array( '.' => '', '-' => '', ' ' => '' ) );

            $ext = '';
            if ( !empty( $_FILES[ 'logo' ] ) ) {
                $parts = explode( '.', $_FILES[ 'logo' ][ 'name' ] );
                $ext = strtoupper( end( $parts ) );
            }

            $this->_setFormData( $_POST[ 'fiscal_id' ], $_POST[ 'name' ],
                    $_POST[ 'commercial_name' ], $ext );

            $e->cancelBubble();
        } else if ( $e->tag == 'eliminar' ) $e->cancelBubble();
    }

    function doPOST( Event $e )
    {
        if ( $e->tag == 'crear' ) $this->crear();
    }

    function handleGET( Event $e )
    {
        if ( $e->tag == 'busqueda' ) {

            $_GET[ 'name' ] = trim( preg_replace( '/\s+?/', ' ', $_GET[ 'name' ] ) );
            $_GET[ 'fiscal_id' ] = strtr( $_GET[ 'fiscal_id' ],
                    array( '.' => '', '-' => '', ' ' => '' ) );

            $this->setSearchData( $_GET[ 'fiscal_id' ], $_GET[ 'name' ] );
        }
        else parent::handleGET( $e );
    }

    function fill()
    {
        if ( $this->search_name != '' ) {
            $partes = explode( ' ', $this->search_name );
            foreach ( $partes as &$p ) $p = MyLink::quote_lgr( $p, '%', '%' );

            if ( !empty( $partes ) )
                    $this->andWhere( 'name LIKE ' . implode( ' AND name LIKE ',
                                $partes ) . ' OR commercial_name LIKE ' . implode( ' AND commercial_name LIKE ',
                                $partes ) );
        }

        if ( !empty( $this->search_fiscal_id ) )
                $this->andWhere( 'fiscal_id LIKE ' . MyLink::quote_lgr( $this->search_fiscal_id,
                            '', '%' ) );

        parent::fill();
    }

}

?>
